﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class LoaiNhanVienListView : Form
    {
        private BindingList<LoaiNhanVien> bindingListLoaiNhanVien;
        private LoaiNhanVien _loaiNhanvien;

        public LoaiNhanVienListView()
        {
            InitializeComponent();
        }

        private void LoaiNhanVienListView_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[] { "MaQuanLy", "TenLoaiNhanVien" };
            string[] listColumnName = new string[]{"Mã Quản Lý", "Tên Loại Nhân Viên"};
            AppGridView.InitGridView(gvLoaiNhanVien,false,false,DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect,false,false,false,true);
            AppGridView.ReadOnlyGridView(gvLoaiNhanVien);
            AppGridView.ShowField(gvLoaiNhanVien,listFieldName,listColumnName);

            LoadData();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _loaiNhanvien = new LoaiNhanVien();
            LoaiNhanVienDetailView detailView = new LoaiNhanVienDetailView(_loaiNhanvien);
            OpenDetailForm(detailView);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }
        private void gcLoaiNhanVien_DoubleClick(object sender, EventArgs e)
        {
            _loaiNhanvien = gvLoaiNhanVien.GetFocusedRow() as LoaiNhanVien;
            if (_loaiNhanvien != null)
            {
                LoaiNhanVienDetailView detailView = new LoaiNhanVienDetailView(_loaiNhanvien);
                OpenDetailForm(detailView);
            }
        }

        private void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _loaiNhanvien = gvLoaiNhanVien.GetFocusedRow() as LoaiNhanVien;

                if (_loaiNhanvien != null && _loaiNhanvien.Oid > 0)
                
                    if (DataAccessObject.LoaiNhanVien.Delete(_loaiNhanvien) > 0)
                    {
                        gvLoaiNhanVien.DeleteSelectedRows();
                        gvLoaiNhanVien.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
        }
        private void LoadData()
        {
            bindingListLoaiNhanVien = new BindingList<LoaiNhanVien>(DataAccessObject.LoaiNhanVien.GetListLoaiNhanVien());

            bindingSourceLoaiNhanVien.DataSource = bindingListLoaiNhanVien;
            gvLoaiNhanVien.Invalidate();

        }
        private void OpenDetailForm(LoaiNhanVienDetailView detailView)
        {
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.DeleteChanged += new EventHandler(detailView_DeleteChanged);
            detailView.ShowDialog();
        }

        void detailView_DeleteChanged(object sender, EventArgs e)
        {
            DeleteRow();
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            bindingListLoaiNhanVien.Add(_loaiNhanvien);
            gvLoaiNhanVien.Invalidate();
        }

    }
}
